//tag::recents[]
package tacos.web.api;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.EntityLinks;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
//end::recents[]
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
//tag::recents[]
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import tacos.Taco;
import tacos.data.TacoRepository;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

@RestController
@RequestMapping(path = "/design",                      // <1>
        produces = "application/json")
@CrossOrigin(origins = "*")        // <2>
public class DesignTacoController {
    private TacoRepository tacoRepo;

    @Autowired
    EntityLinks entityLinks;

    public DesignTacoController(TacoRepository tacoRepo) {
        this.tacoRepo = tacoRepo;
    }

    @GetMapping("/recent")
    public Iterable<Taco> recentTacos() {                 //<3>
        PageRequest page = PageRequest.of(
                0, 12, Sort.by("createdAt").descending());
        return tacoRepo.findAll(page).getContent();
    }
    //end::recents[]
    
    @GetMapping("/recentfs")
    public CollectionModel<EntityModel<Taco>> recentTacosFS() {
        PageRequest page = PageRequest.of(
                0, 12, Sort.by("createdAt").descending());
        List<Taco> tacos = tacoRepo.findAll(page).getContent();

        CollectionModel<EntityModel<Taco>> tacosResources = CollectionModel.wrap(tacos);

        tacosResources.add(
                linkTo(DesignTacoController.class)
                    .slash("recentfs")
                    .withRel("recents"));
        return tacosResources;
    }

    @GetMapping("/recentf")
    public CollectionModel<EntityModel<Taco>> recentTacosF() {
        PageRequest page = PageRequest.of(
                0, 12, Sort.by("createdAt").descending());
        List<Taco> tacos = tacoRepo.findAll(page).getContent();

        CollectionModel<EntityModel<Taco>> tacosResources = CollectionModel.wrap(tacos);

        tacosResources.add(
                linkTo(methodOn(DesignTacoController.class).recentTacosF())
                        .withRel("recents"));
        return tacosResources;
    }


    @GetMapping("/recenth")
    public CollectionModel<TacoResource> recentTacosH() {
        PageRequest page = PageRequest.of(
                0, 12, Sort.by("createdAt").descending());
        List<Taco> tacos = tacoRepo.findAll(page).getContent();

        CollectionModel<TacoResource> tacosResources =
                new TacoResourceAssembler().toCollectionModel(tacos);

        // CollectionModel<TacoResource> recentResources = CollectionModel.of(tacoResources);

        tacosResources.add(
                linkTo(methodOn(DesignTacoController.class).recentTacosH())
                        .withRel("recents"));
        return tacosResources;
    }


//ControllerLinkBuilder.linkTo(DesignTacoController.class)
//.slash("recent")
//.withRel("recents"));


    @GetMapping("/recenth2")
    public CollectionModel<TacoResource> recenthTacos() {
        PageRequest page = PageRequest.of(
                0, 12, Sort.by("createdAt").descending());
        List<Taco> tacos = tacoRepo.findAll(page).getContent();

        CollectionModel<TacoResource> tacosResources = new TacoResourceAssembler().toCollectionModel(tacos);
        
        Link recentsLink = 
            linkTo(DesignTacoController.class)
            .slash("recenth2")
            .withRel("recents_slash");

        Link recentsLink1 = 
            linkTo(methodOn(DesignTacoController.class).recenthTacos())
            .withRel("recents_methodOn");

        tacosResources.add(recentsLink);
        tacosResources.add(recentsLink1);
        return tacosResources;
    }

    //tag::postTaco[]
    @PostMapping(consumes = "application/json")
    @ResponseStatus(HttpStatus.CREATED)
    public Taco postTaco(@RequestBody Taco taco) {
        return tacoRepo.save(taco);
    }
    //end::postTaco[]


    @GetMapping("/{id}")
    public Taco tacoById(@PathVariable("id") Long id) {
        Optional<Taco> optTaco = tacoRepo.findById(id);
        if (optTaco.isPresent()) {
            return optTaco.get();
        }
        return null;
    }

//  @GetMapping("/{id}")
//  public ResponseEntity<Taco> tacoById(@PathVariable("id") Long id) {
//    Optional<Taco> optTaco = tacoRepo.findById(id);
//    if (optTaco.isPresent()) {
//      return new ResponseEntity<>(optTaco.get(), HttpStatus.OK);
//    }
//    return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
//  }


//tag::recents[]
}
//end::recents[]

